package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.ui.adapter.ImageViewerAdapter;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

/**
 * Created by WuFan on 2017/10/28.
 */

public class ImageActivity extends AutoLayoutActivity {
    private ImageView mIvBack;

    public static final String CURRENT_POS = "CURRENT_POS";
    public static final String IMAGE_PATH_LIST = "IMAGE_PATH_LIST";

    //private TextView mTvComplete;
    private TextView mTvTitle;
    private ViewPager mImageViewPager;
    private ArrayList<Integer> mImagePaths;
    private int mCurrentPosition;
    private ImageViewerAdapter mImageViewerAdapter;
    private TextView mTvSure;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        Intent intent = getIntent();
        mCurrentPosition = intent.getIntExtra(CURRENT_POS, 0);
        mImagePaths = intent.getIntegerArrayListExtra(IMAGE_PATH_LIST);
        initView();
        initData();

        mImageViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 1){
                    mTvSure.setVisibility(View.VISIBLE);
                }else{
                    mTvSure.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private void initView() {
        mImageViewPager = (ViewPager) findViewById(R.id.vp);
        mTvSure = (TextView) findViewById(R.id.tv_sure);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText(mCurrentPosition+1 + "/" + mImagePaths.size());
        mImageViewerAdapter = new ImageViewerAdapter(mImagePaths);
        mImageViewPager.setAdapter(mImageViewerAdapter);
        mImageViewPager.setCurrentItem(mCurrentPosition);
        mImageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
                mTvTitle.setText( mCurrentPosition+1 + "/" + mImagePaths.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }

    private void initData() {

    }

    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_sure) {
            finish();
        }
    }
}
